package com.sisSaude.daos;

import java.sql.ResultSet;
import java.util.Vector;

import com.sisSaude.entityBeans.Aluno;
import com.sisSaude.enumerations.Sexo;
import com.sisSaude.utils.DataProvider;
import com.sisSaude.utils.SqlFactory;

public class AlunoDAO extends GenericDAO<Aluno> {

	// fields
	public static String SELECT_BASICA = " SELECT * " +
			                             " FROM Aluno";
	
	// public methods
	public boolean canDelete(Aluno aluno) {
		DataProvider dataProvider = new DataProvider();
		return dataProvider.canDelete("Aluno", aluno.getId());
	}
	public boolean delete(Aluno aluno) {
		String sql = SqlFactory.getSqlDelete("Aluno", "id");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(aluno.getId());
		try {
			dataProvider.execute(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
		return true;
	}
	public Aluno get(int id) {
		Aluno aluno = null;
		String sql = SELECT_BASICA + " WHERE Aluno.id = ? "; 
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(id);
		try {
			dataProvider.select(sql, params);
			if (dataProvider.next()) {
				aluno = new Aluno();
				loadAttribs(aluno, dataProvider.getResultSet());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			dataProvider.close();
		}
		return aluno;
	}
	public void loadAttribs(Aluno aluno, ResultSet r) {
		try {
			aluno.setId(r.getInt("id"));
			aluno.setIdUsuario(r.getInt("idUsuario"));
			aluno.setNome(r.getString("nome"));
			aluno.setCodigoCatraca(r.getInt("codigoCatraca"));
			aluno.setDataInicio(r.getDate("dataInicio"));
			aluno.setTelefoneResidencial(r.getString("telefoneResidencial"));
			aluno.setTelefoneCelular(r.getString("telefoneCelular"));
			aluno.setDataDeNascimento(r.getDate("dataDeNascimento"));
			aluno.setSexo(Sexo.valueOf(r.getString("sexo")));
			aluno.setProfissao(r.getString("profissao"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// protected methods
	protected boolean insert(Aluno aluno) {
		String sql = SqlFactory.getSqlInsert("Aluno");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(aluno.getId());
		params.add(aluno.getIdUsuario());
	 	params.add(aluno.getNome());
	 	params.add(aluno.getCodigoCatraca());
	 	params.add(aluno.getDataInicio());
	 	params.add(aluno.getTelefoneResidencial());
		params.add(aluno.getTelefoneCelular());
		params.add(aluno.getDataDeNascimento());
		params.add(aluno.getSexo().name());
		params.add(aluno.getProfissao());
		try {
			aluno.setId(dataProvider.insert(sql, params));
			return aluno.getId() > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
	}
	protected boolean update(Aluno aluno) {
		String sql = SqlFactory.getSqlUpdate("Aluno", "Aluno.id");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(aluno.getIdUsuario());
	 	params.add(aluno.getNome());
	 	params.add(aluno.getCodigoCatraca());
	 	params.add(aluno.getDataInicio());
	 	params.add(aluno.getTelefoneResidencial());
		params.add(aluno.getTelefoneCelular());
		params.add(aluno.getDataDeNascimento());
		params.add(aluno.getSexo().name());
		params.add(aluno.getProfissao());
		params.add(aluno.getId());
		try {
			dataProvider.execute(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
		return true;
	}

}
